<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 通用工具栏组件使用 -->
      <page-tools>
        <!-- 自定义左侧内容 -->
        <template #slot-left>
          <span>共 {{ total }} 条记录</span>
        </template>

        <!-- 自定义右侧内容 -->
        <template #slot-right>
          <el-button type="danger" size="small">导入excel</el-button>
          <el-button type="success" size="small">导出excel</el-button>
          <el-button type="primary" size="small" @click="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <!-- 表格区域 -->
        <el-table border :data="employeesList" :default-sort="{prop: 'workNumber'}">
          <el-table-column label="序号" type="index" />
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="staffPhoto" label="头像" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式" :formatter="formatter" />
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column prop="correctionTime" label="转正时间" sortable />
          <el-table-column label="操作" width="280">
            <template slot-scope="scope">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="delEmp(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页区域 -->
          <el-pagination
            :current-page="query.page"
            :page-sizes="[10, 15, 20, 25]"
            :page-size="query.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
        <!-- 新增员工弹框组件 -->
        <el-dialog title="新增员工" :visible.sync="showDialog">
          <emp-dialog @close="showDialog=false" @update-list="updateList" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import EmployeeEnum from '@/constant/employees'
import { getEmployeeList, delEmployee } from '@/api/employees'
import EmpDialog from './empDialog.vue'
export default {
  components: { EmpDialog },
  data() {
    return {
      query: {
        page: 1, // 页码
        size: 10 // 每页条数
      },
      total: 0, // 数据总条数
      employeesList: [], // 员工列表
      showDialog: false
    }
  },
  created() {
    this.getEmployeeList()
  },
  methods: {
    // 添加员工后刷新列表
    updateList() {
      this.getEmployeeList()
      this.showDialog = false
    },
    // 删除员工
    async delEmp(id) {
      // 询问框
      const confirmRes = await this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 处理取消
      if (confirmRes === 'cancel') return this.$message('您取消了删除')
      // 处理确定 调接口
      const res = await delEmployee(id)
      if (this.employeesLis.length === 1) {
        this.query.page--
        if (this.query.page <= 0) {
          this.query.page = 1
        }
      }
      this.$message.success(res.message)
      this.getEmployeeList()
    },

    // 格式化聘用形式  枚举
    formatter(row, column, cellValue, index) {
      const obj = EmployeeEnum.hireType.find(item => item.id === Number(cellValue))
      return obj ? obj.value : '未知'
    },
    // 获取员工列表
    async getEmployeeList() {
      const res = await getEmployeeList(this.query)
      // console.log(res)
      this.employeesList = res.data.rows
      this.total = res.data.total
    },
    // 当每页显示条数发生改变时触发
    handleSizeChange(newSize) {
      this.query.size = newSize
      this.getEmployeeList()
    },
    // 当页码发生改变时触发
    handleCurrentChange(newPage) {
      this.query.page = newPage
      this.getEmployeeList()
    }
  }

}
</script>

<style lang="scss" scoped></style>
